package com.sandu.lib_common.utils

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.sandu.lib_common.base.BaseApplication
import com.sandu.lib_common.ext.jumpByLogin
import com.sandu.lib_common.ui.MainActivity

open class JumpUtils {
    companion object {
        open fun jumpTo(
            context: Context,
            targetActivity: Class<*>,
            vararg extras: Pair<String, Any>
        ) {
            val intent = Intent(context, targetActivity)
            for (extra in extras) {
                when (extra.second) {
                    is Int -> intent.putExtra(extra.first, extra.second as Int)
                    is String -> intent.putExtra(extra.first, extra.second as String)
                    is Boolean -> intent.putExtra(extra.first, extra.second as Boolean)
                    // 根据需要添加其他类型的处理
                }
            }
            context.startActivity(intent)
        }

        open fun jumpTo(
            activity: Activity,
            targetActivity: Class<*>,
            vararg extras: Pair<String, Any>
        ) {
            val intent = Intent(activity, targetActivity)
            for (extra in extras) {
                when (extra.second) {
                    is Int -> intent.putExtra(extra.first, extra.second as Int)
                    is String -> intent.putExtra(extra.first, extra.second as String)
                    is Boolean -> intent.putExtra(extra.first, extra.second as Boolean)
                    // 根据需要添加其他类型的处理
                }
            }
            activity.startActivity(intent)
        }

        open fun jumpTo(
            activity: FragmentActivity,
            targetActivity: Class<*>,
            vararg extras: Pair<String, Any>
        ) {
            val intent = Intent(activity, targetActivity)
            for (extra in extras) {
                when (extra.second) {
                    is Int -> intent.putExtra(extra.first, extra.second as Int)
                    is String -> intent.putExtra(extra.first, extra.second as String)
                    is Boolean -> intent.putExtra(extra.first, extra.second as Boolean)
                    // 根据需要添加其他类型的处理
                }
            }
            activity.startActivity(intent)
        }

        open fun jumpToByLogin(
            context: Context,
            targetActivity: Class<*>,
            vararg extras: Pair<String, Any>
        ) {
            if (CacheUtil.isLogin()) {
                val intent = Intent(context, targetActivity)
                for (extra in extras) {
                    when (extra.second) {
                        is Int -> intent.putExtra(extra.first, extra.second as Int)
                        is String -> intent.putExtra(extra.first, extra.second as String)
                        is Boolean -> intent.putExtra(extra.first, extra.second as Boolean)
                        // 根据需要添加其他类型的处理
                    }
                }
                context.startActivity(intent)
            } else {
                val intent = Intent(context, getApplication(context).activityLogin)
                context.startActivity(intent)
            }


        }

        open fun jumpToByLogin(
            context: Activity,
            targetActivity: Class<*>,
            vararg extras: Pair<String, Any>
        ) {
            if (CacheUtil.isLogin()) {
                val intent = Intent(context, targetActivity)
                for (extra in extras) {
                    when (extra.second) {
                        is Int -> intent.putExtra(extra.first, extra.second as Int)
                        is String -> intent.putExtra(extra.first, extra.second as String)
                        is Boolean -> intent.putExtra(extra.first, extra.second as Boolean)
                        // 根据需要添加其他类型的处理
                    }
                }
                context.startActivity(intent)
            } else {
                val intent = Intent(context, getApplication(context).activityLogin)
                context.startActivity(intent)
            }


        }

        open fun jumpToByLogin(
            context: FragmentActivity,
            targetActivity: Class<*>,
            vararg extras: Pair<String, Any>
        ) {
            if (CacheUtil.isLogin()) {
                val intent = Intent(context, targetActivity)
                for (extra in extras) {
                    when (extra.second) {
                        is Int -> intent.putExtra(extra.first, extra.second as Int)
                        is String -> intent.putExtra(extra.first, extra.second as String)
                        is Boolean -> intent.putExtra(extra.first, extra.second as Boolean)
                        // 根据需要添加其他类型的处理
                    }
                }
                context.startActivity(intent)
            } else {
                val intent = Intent(context, getApplication(context).activityLogin)
                context.startActivity(intent)
            }


        }

        fun getApplication(context: Context): BaseApplication {
            return context.applicationContext as BaseApplication
        }


    }
}